
#ifndef __COID_SERVER_ACCOUNTMGR__HEADER_FILE__
#define __COID_SERVER_ACCOUNTMGR__HEADER_FILE__

#include "coid/svc/coidsvc.h"
#include "coid/coid.h"
#include "coid/comm/hash/hashkeyset.h"


namespace coid {


////////////////////////////////////////////////////////////////////////////////
///COID account manager service
class AccountMgr
{
    //NET_OPTIONS: 

    struct account_id_hash
    {
        typedef account_id      key_type;

        size_t operator() ( const account_id& acc ) const
        {
            return __coid_hash_string( acc._name.ptr(), acc._name.len() )
                ^ __coid_hash_string( acc._domain.ptr(), acc._domain.len() );
        }
    };


    cmd_interface* _cmdin;

    typedef hash_keyset<account, _Select_GetRef<account,account_id>, account_id_hash>     t_hash;
    t_hash  _accounts;

    uint _acid;     ///seq.number

public:

    AccountMgr()  { _acid=0; }

    // special functions

    net_fnc_s opcd accept_startup_params( cmd_interface& cmd )
    {
        _cmdin = &cmd;
        return 0;
    }

    net_fnc_s opcd accept_connect()
    {
        return ersDENIED;
    }
    
    net_fnc_s opcd accept_connect_shared()
    {
        return 0;
    }

    net_fnc_s opcd accept_connect_auto()
    {
        load();
        _cmdin->set_option( cmd_interface::OPTION_ALLOW_SHARED_ACCESS, 1 );
        return 0;
    }

    net_fnc_s opcd accept_destroy()
    {
        save();
        return 0;
    }

    // client functions

    net_fnc opcd add_account( net_inout account& acid, bool modify = false );
    net_fnc opcd del_account( net_in const charstr& name, net_in const charstr& key );

    net_fnc opcd find_account( net_inout account& acid ) const;

    net_fnc opcd list_accounts( net_out dynarray<account>& data ) const;


private:
    opcd save() const;
    opcd load();
};

} // namespace coid

#endif //__COID_SERVER_ACCOUNTMGR__HEADER_FILE__

